package com.liao.furn.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liao.furn.pojo.Furn;
import com.liao.furn.service.FurnService;
import com.liao.furn.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
@Slf4j
@RestController
public class FurnController {
    @Resource
    private FurnService furnService;

    @PostMapping("/save")
    public Result save(@Validated  @RequestBody  Furn furn, Errors errors){
        HashMap<String, Object> map = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }
        if(map.isEmpty()){
            log.info("furn={}",furn);
            furnService.save(furn);
            return Result.success();
        }else {
            return Result.error("400","后端校验失败",map);
        }
    }

    @RequestMapping("/furns")
    public Result listFurn(){
        List<Furn> furnList = furnService.list();

        return Result.success(furnList);
    }

    @PutMapping("/update")
    public Result updateFurn(@RequestBody  Furn furn){
        furnService.updateById(furn);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteFurn(@PathVariable Integer id){
        furnService.removeById(id);
        return Result.success();
    }

    @GetMapping("/select/{id}")
    public Result selectFurnById(@PathVariable Integer id){
        Furn furn = furnService.getById(id);
        log.info("furn={}",furn);
        return Result.success(furn);
    }

    @GetMapping("/pageFurn")
    public Result pageFurn(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize){
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize));
        return Result.success(page);
    }

    @GetMapping("/pageFurnByCondition")
    public Result pageFurnByCondition(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,@RequestParam(defaultValue = "") String search){
        QueryWrapper<Furn> queryWrapper = Wrappers.query();
        if(StringUtils.hasText(search)){
            queryWrapper.like("name",search);
        }
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return Result.success(page);
    }

    @GetMapping("/pageFurnByCondition2")
    public Result pageFurnByCondition2(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "5") Integer pageSize,@RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Furn> lambdaQueryWrapper = Wrappers.<Furn>lambdaQuery();
        if(StringUtils.hasText(search)){
            lambdaQueryWrapper.like(Furn::getName,search);
        }
        Page<Furn> page = furnService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        log.info("page={}",page.getRecords());
        return Result.success(page);
    }
}
